export default {

  on: function(eventName, obj) {
    this.eventList = this.eventList || {}
    this.eventList[eventName] = this.eventList[eventName] || []
    this.eventList[eventName].push(obj)
  },

  off: function(eventName, obj) {
    if (this.eventList && this.eventList[eventName]) {
      var index = this.eventList[eventName].indexOf(obj)
      if (index !== -1) {
        this.eventList[eventName].splice(index, 1)
      }
    }
  },

  emit: function(eventName, ...rest) {
    if (this.eventList && this.eventList[eventName]) {
      var objAry = this.eventList[eventName]
      for (var i = 0; i !== objAry.length; i++) {
        var obj = objAry[i]
        obj[eventName].apply(obj, rest)
      }
    }
  },

  onOnce: function(eventName, obj) {
    this.eventListOnce = this.eventListOnce || {}
    this.eventListOnce[eventName] = this.eventListOnce[eventName] || []
    this.eventListOnce[eventName].push(obj)
  },

  offOnce: function(eventName, obj) {
    if (this.eventListOnce && this.eventListOnce[eventName]) {
      var index = this.eventListOnce[eventName].indexOf(obj)
      if (index !== -1) {
        this.eventListOnce[eventName].splice(index, 1)
      }
    }
  },

  emitOnce: function(eventName, ...rest) {
    if (this.eventListOnce && this.eventListOnce[eventName]) {
      const objAry = this.eventListOnce[eventName]
      for (let i = objAry.length - 1; i >= 0; i--) {
        const obj = objAry[i]
        if (obj[eventName].apply(obj, rest)) return
      }
    }
  }
}
